package de.koller.nature.proto;

import de.koller.oceanFramework.gl.util.ImagePreview;
import de.koller.oceanFramework.util.ColorIntUtil;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;

public class Terrain {

	public static void main( String[] args ) {
		while( true )
			terrain();
	}

	private static void terrain() {
		Random rnd = new Random();
		final int fw = 4096, fh = 4096;
		final int checkSize = 64;
		
		BufferedImage img = new BufferedImage( fw, fh, BufferedImage.TYPE_INT_ARGB );
		int[][] tp = new int[ fw + 2*checkSize ][ fh + 2*checkSize ];
		int[][] th = new int[ fw ][ fh ];
		
		for( int x = 0; x < tp.length; x++ )
			for( int y = 0; y < tp[0].length; y++ ) {
				int t = rnd.nextInt( 2 );
				tp[x][y] = t;
			}
		
		int max = 0;
		int min = Integer.MAX_VALUE;
		for( int x = 0; x < fw; x++ ) {
			for( int y = 0; y < fh; y++ ) {
				for( int dx = -checkSize; dx < checkSize; dx++ )
					for( int dy = -checkSize; dy < checkSize; dy++ ) {
						double dis = Math.sqrt( dx*dx + dy*dy );
						if( dis < checkSize )
							th[x][y] += tp[ x + checkSize + dx ][ y + checkSize + dy ];
					}
				max = Math.max( th[x][y], max );
				min = Math.min( th[x][y], min );
			}
			System.out.println( x );
		}
		
		double c = 255. / (max - min);
		for( int x = 0; x < th.length; x++ )
			for( int y = 0; y < th[0].length; y++ ) {
				int k = (int)((th[x][y] - min) * c);
				img.setRGB( x, y, ColorIntUtil.getIntColor( 0xff, k, k, k ) );
			}
		
		try {
			ImageIO.write( img, "png", new File( "./output/nature/terrain.png" ) );
		} catch( IOException ex ) { System.err.println( ex ); }
		ImagePreview.showImage( img, fw, fh );
	}
	
}
